package com.ums.ribbonconsumer.service;

import feign.Contract;
import feign.RequestLine;
import feign.Retryer;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.web.http.CookieHttpSessionStrategy;
import org.springframework.session.web.http.HttpSessionStrategy;

import java.util.Map;

import static java.util.concurrent.TimeUnit.SECONDS;

//为这个服务的所有请求设置header
//@Headers({"Content-Type:application/json","Auth:98741908273012343214"})
@FeignClient(value = "hello-service", configuration = FeignConfig.class)
public interface HelloService {
    @RequestLine("GET /hello")
    String hello();

    //单独给某个请求设置header
    //@Headers({"Content-Type:application/json","Auth:198741908273012343214"})
    @RequestLine("POST /placeOrder")
    String placeOrder(String body);

    @RequestLine("GET /get_session")
    Map getSession();
}

@Configuration
class FeignConfig{
    @Bean
    public Contract feignConfiguration(){
        return new feign.Contract.Default();
    }
    @Bean
    public Retryer feignRetryer() {
        return new Retryer.Default(100, SECONDS.toMillis(1), 5);
    }

    @Bean
    public HttpSessionStrategy httpSessionStrategy() {
        //return new HeaderHttpSessionStrategy();
        return new CookieHttpSessionStrategy();
    }
}
